gene<-read.table("merge_expr_filtered",sep="\t",head =T)
anno<-read.table("file_final_ucscnew",sep="\t")
geno<-read.table("combined_GBM_normal_plink_final_filetered_het_minhomo_10.tped",sep=" ")
rownames(anno)<-as.vector(anno[,1])
gene1<-merge(anno,gene,by="row.names",all.x=F)
i<-1
samplesize<-20
num<-as.integer((length(gene1[1,])-4)/samplesize)
predgene<-matrix(nrow=nrow(gene1), ncol=ncol(gene1)-4)
while(i<nrow(gene1)+1)
{
        if(gene1[i,3] == "+"){l1 = gene1[i,4]-30000;l2=gene1[i,4]+20000}else{l1 = gene1[i,4]-20000;l2=gene1[i,4]+30000}
        index<-which(geno[,4]>=l1 & geno[,4]<l2)
        geno1<-geno[index,]
        eqtlgene<-as.matrix(gene1[i,5:ncol(gene1)])
        eqtlgeno<-as.matrix(geno1[8:ncol(geno1)])


	d<-1
	while(d<num+1)
	{			
		s<-d;
		sub<-c()
		main<-c()
		if(d > 1)
		{
			main<-seq(1,d-1,1)
		}
		nu<-1
		while(s<ncol(eqtlgene)+1)
		{
			sub[nu]<-s
			sequ<-c()		
			if(s+1 <= ncol(eqtlgene))
			{	
				sequ<-seq(s+1,s+num-1,1)
			
				if(s+num-1 > ncol(eqtlgene))
				{
					sequ<-seq(s+1,ncol(eqtlgene),1)
				}
			}
			main<-c(main,sequ)
			nu<-nu+1	
			s<-s+num
		}
		geneeqtl<-t(as.matrix(eqtlgene[,main]))
		geneeqtlsub<-t(as.matrix(eqtlgene[,sub]))
                genomaineqtl<-eqtlgeno[,main]
                genosubeqtl<-eqtlgeno[,sub]
		pred1<-c()
		rms<-c()
		u<-1
		
		while(u<nrow(genomaineqtl)+1)
		{
			d1 <- data.frame(geneeqtl[1,],genomaineqtl[u,])
                	colnames(d1)<-c("gene","genotype")
                	glm1 <- glm(gene ~ genotype, data=d1,family=gaussian(link="log"))
                	vec1<-as.data.frame(as.vector(genosubeqtl[u,]))
               	 	colnames(vec1)<-c("genotype")
                	pred <- predict(glm1, data.frame(cbind(genotype=vec1)), type="response")
			pred1<-rbind(pred1,pred)
			rms[u]= 0;
			s<-1
			while(s<length(pred)+1)
			{
				rms[u] = rms[u]+((pred[s]-geneeqtlsub[1,s])*(pred[s]-geneeqtlsub[1,s]));	
				s<-s+1
			}
			u<-u+1	
		}
		nn <-which(rms == min(rms))
		t<-1
                while(t<length(sub)+1)
                {
                	predgene[i,sub[t]]<-pred1[nn,t]
                        #print(pred[i,])
                        t<-t+1
                }

		d<-d+1
	}
	print(i)
	i<-i+1
}

